import { Component, OnInit, ViewEncapsulation, Injector } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    CostAccountingServiceProxy,
    NotAccountedCountResult,
    ComboboxItemDto,
    Workshop
} from '@shared/service-proxies/service-proxies';

@Component({
    templateUrl: './cost-accounting.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class CostAccountingComponent extends AppComponentBase implements OnInit {
    countResult = new NotAccountedCountResult();

    workShop: number;
    workShopComboboxs = new Array<ComboboxItemDto>();

    selectedDate = new Date();

    isLoading1 = false;
    isLoading2 = false;

    constructor(injector: Injector, private _accountingService: CostAccountingServiceProxy) {
        super(injector);
    }

    ngOnInit(): void {
        this.workShop = Workshop.Machine;
        this.workShopComboboxs = this.getWorkshops();
        this._accountingService
            .getNotAccountedCount(this.workShop, this.selectedDate.getFullYear(), this.selectedDate.getMonth() + 1)
            .subscribe(result => {
                this.countResult = result;
            });
    }

    selectChange() {
        this._accountingService
            .getNotAccountedCount(this.workShop, this.selectedDate.getFullYear(), this.selectedDate.getMonth() + 1)
            .subscribe(result => {
                this.countResult = result;
            });
    }

    actualAccounting() {
        this.isLoading1 = true;
        this._accountingService
            .countCurrentMonthActualCosts(
                this.workShop,
                this.selectedDate.getFullYear(),
                this.selectedDate.getMonth() + 1
            )
            .subscribe(result => {
                this.isLoading1 = false;
                this.notify.info('间接成本计算成功');
            });
    }

    directAccounting() {
        this.isLoading2 = true;
        this._accountingService
            .countCurrentMonthDirectCosts(
                this.workShop,
                this.selectedDate.getFullYear(),
                this.selectedDate.getMonth() + 1
            )
            .subscribe(result => {
                this.isLoading2 = false;
                this.notify.info('直接成本计算成功');
            });
    }
}
